//
//  HonourViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/2/14.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "HonourViewController.h"
#import "HonourTableViewCell.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
@interface HonourViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableV;
@property (strong,nonatomic) NSArray *darr;
@end

@implementation HonourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"荣誉";
    _darr = [NSArray array];
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    [_tableV registerNib:[UINib nibWithNibName:@"HonourTableViewCell" bundle:nil] forCellReuseIdentifier:@"HonourTableViewCell"];
    [_tableV setTableFooterView:[UIView new]];
    [self getteacherglorydetail];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)getteacherglorydetail{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APISearch@"teacher/getteacherglorydetail.json" parameters:@{@"teacherId":_teacherId} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            _darr = [responseObject objectForKey:@"result"];
            [_tableV reloadData];        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _darr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *tmp = [[_darr objectAtIndex:indexPath.row] objectForKey:@"imgUrl"];
    if (tmp.count) {
        return 114;
    }else{
        return 52;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tmp = [_darr objectAtIndex:indexPath.row];
    HonourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HonourTableViewCell"];
    cell.content.text = [NSString stringWithFormat:@"%@ %@",[tmp objectForKey:@"text"],[tmp objectForKey:@"gloryName"]];
    cell.imgs = [tmp objectForKey:@"imgUrl"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HonourTableViewCell  *cell = (HonourTableViewCell  *)[tableView  cellForRowAtIndexPath:indexPath];
    CGRect rect = [cell convertRect:cell.imageV.frame toView:self.view.window];
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    NSArray *dataArray =[[_darr objectAtIndex:indexPath.row] objectForKey:@"imgUrl"];
    NSInteger  count = [dataArray  count];
    for (int i = 0; i<count; i++) {
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.firstFrame = rect;
        NSString  *tempDic = [dataArray  objectAtIndex:i];

            photo.url = [NSString  stringWithFormat:@"%@",tempDic]; // 图片路径

        photo.srcImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Placeholder200_200"]]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.isShowRight = NO;
    browser.photos = photos; // 设置所有的图片
    browser.currentPhotoIndex = 0;
    [browser show];
}

@end
